package com.mohan.json;

import java.util.ArrayList;
import java.util.List;

import javax.json.JsonArray;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mohan.pojo.Article;
import com.mohan.pojo.Comment;
import com.mohan.pojo.Content;
import com.mohan.pojo.Img;
import com.mohan.pojo.Reply;
import com.sun.prism.paint.Color;

public class ArticleParse {
	
	/**
	 * 向数据库导入文章
	 * @param jsonStr(article文章的json字符串)
	 * @return
	 */
	public Object addArticle(String jsonStr){
		JSONObject obj = new JSONObject(jsonStr);
		
		String author = obj.getString("author");
		String book = obj.getString("book");
		String dynasty = obj.getString("dynasty");
		String articleprofile = obj.getString("articleProfile");
		String tags = obj.getString("tags");
		String style = obj.getString("style");
		int type = obj.getInt("type");
		int loves = obj.getInt("loves");
		String analysis = obj.getString("analysis");
		JSONArray contents = obj.getJSONArray("contents");
		JSONArray comments = obj.getJSONArray("comments");
		int bookid = obj.getInt("bookid");
		String userid = obj.getString("userid");
		int examine = obj.getInt("examine");
		int articlestatus = obj.getInt("articleStatus");
		int recommend = obj.getInt("recommend");
		int isopen = obj.getInt("isopen");
		String title = obj.getString("title");
		
		/**
		 * 获取json字符串里的content(段落)和img(插图)
		 */
		List<Content> contentList = new ArrayList<Content>();
		List<Img> imgList = new ArrayList<Img>();
		if(contents != null){
			for(int i=0 ; i<contents.length() ; i++){
				Content content = new Content();
				JSONObject contentJson = (JSONObject) contents.get(i);
				content.setFontColor(contentJson.getString("fontColor"));
				content.setFontSize(contentJson.getInt("fontSize"));
				content.setAudioUrl(contentJson.getString("audioUrl"));
				content.setVideoUrl(contentJson.getString("videoUrl"));
				content.setSerialNum(contentJson.getInt("serialNum"));
				content.setArticleId(contentJson.getInt("articleId"));
				content.setAuthor(contentJson.getString("author"));
				content.setUserId(contentJson.getString("userid"));
				content.setContent(contentJson.getString("content"));
				
				JSONArray imgs = contentJson.getJSONArray("imgs");
				if (imgs != null) {
					for(int j=0 ; j<imgs.length() ; j++){
						Img img = new Img();
						JSONObject imgJson = (JSONObject) imgs.get(j);
						img.setImgUrl(imgJson.getString("imgurl"));
						img.setImgPosition(imgJson.getInt("imgposition"));
						img.setImgSize(imgJson.getString("imgsize"));
						img.setUserId(imgJson.getString("userid"));
						img.setContentId(imgJson.getInt("contentid"));
						img.setArticleId(imgJson.getInt("articleid"));
						imgList.add(img);
					}
				}
				contentList.add(content);
			}
		}
				
		/**
		 * 提取json字符串里的comment(评论)和reply(评论回复)
		 */
		List<Comment> commentList = new ArrayList<Comment>();
		List<Reply> replyList = new ArrayList<Reply>();
		if (comments != null) {
			for(int i=0 ; i<comments.length() ; i++){
				Comment comment = new Comment();
				JSONObject commentJson = (JSONObject) comments.get(i);
				comment.setArticleId(commentJson.getInt("articleid"));
				comment.setUserId(commentJson.getString("userid"));
				comment.setName(commentJson.getString("name"));
				comment.setLoves(commentJson.getInt("loves"));
				comment.setContent(commentJson.getString("content"));
				comment.setSerialNum(commentJson.getInt("serialnum"));
				JSONArray replys = commentJson.getJSONArray("replys");
				if (replys != null) {
					for(int j=0 ; j<replys.length() ; j++){
						Reply reply = new Reply();
						JSONObject replyJson = (JSONObject) replys.get(j);
						reply.setCommentId(replyJson.getInt("commentid"));
						reply.setUserId(replyJson.getString("userid"));
						reply.setName(replyJson.getString("name"));
						reply.setLoves(replyJson.getInt("loves"));
						reply.setSerialNum(replyJson.getInt("serialnum"));
						reply.setContent(replyJson.getString("content"));
						reply.setArticleId(replyJson.getInt("articleid"));
						replyList.add(reply);
					}
				}
				commentList.add(comment);
			}
		}
				
		
		/**
		 * 提取article
		 */
		Article article = new Article();
		
		article.setAuthor(author);
		article.setBook(book);
		article.setDynasty(dynasty);
		article.setArticleProfile(articleprofile);
		article.setTags(tags);
		article.setStyle(style);
		article.setType(type);
		article.setLoves(loves);
		article.setAnalysis(analysis);
		article.setBookId(bookid);
		article.setUserId(userid);
		article.setExamine(examine);
		article.setArticleStatus(articlestatus);
		article.setRecommend(recommend);
		article.setIsOpen(isopen);
		article.setTitle(title);
			
		return null;
	}
	
	/**
	 * 添加古人诗词
	 * @param jsonStr
	 * @param article
	 * @return
	 */
	public String newArticle2DB(String jsonStr,Article article,List<Content> contentList){
		JSONObject rootJObj = new JSONObject(jsonStr);

		String title = null;
		String author = null;
		String dynasty = null;
		String book = null;
		String articleprofile = null;
		String tags = null;
		String style = null;
		int type = -1;	
		String analysis = null;
		int isopen = -1;
		String userid = null;
		if(rootJObj.has("title")){
			title = rootJObj.getString("title");
		}else {
			return "title tag is not exist!";
		}
		if(rootJObj.has("author")){
			author = rootJObj.getString("author");
		}else {
			author = "佚名";
		}
		if(rootJObj.has("dynasty")){
			dynasty = rootJObj.getString("dynasty");
		}else {
			dynasty =  "未知";
		}
		if(rootJObj.has("book")){
			book = rootJObj.getString("book");
		}else {
			book = "";
		}
		if(rootJObj.has("articleProfile")){
			articleprofile = rootJObj.getString("articleProfile");
		}else {
			articleprofile = "";
		}
		if(rootJObj.has("tags")){
			tags = rootJObj.getString("tags");
		}else {
			tags =  "";
		}
		if(rootJObj.has("style")){
			style = rootJObj.getString("style");
		}else {
			style = "";
		}	
		if(rootJObj.has("type")){
			type = rootJObj.getInt("type");
		}else {
			return "type tag is not exist!";
		}
		if(rootJObj.has("analysis")){
			analysis = rootJObj.getString("analysis");
		}else {
			analysis = "";
		}
		if(rootJObj.has("isOpen")){
			isopen = rootJObj.getInt("isOpen");
		}else {
			isopen = 1;
		}
		if(rootJObj.has("userid")){
			userid = rootJObj.getString("userid");
		}else {
			return "userid tag is not exist!";
		}
		
		/**
		 * 提取article
		 */
		article.setTitle(title);
		article.setAuthor(author);
		article.setDynasty(dynasty);
		article.setBook(book);	
		article.setArticleProfile(articleprofile);
		article.setTags(tags);
		article.setStyle(style);
		article.setType(type);
		article.setAnalysis(analysis);
		article.setIsOpen(isopen);
		article.setUserId(userid);
		System.out.println("---------------------------------");
		System.out.println("title : " + title);
		System.out.println("author : " + author);
		System.out.println("dynasty : " + dynasty);
		System.out.println("book : " + book);
		System.out.println("articleprofile : " + articleprofile);
		System.out.println("tags : " + tags);
		System.out.println("style : " + style);
		System.out.println("type : " + type);
		System.out.println("analysis : " + analysis);
		System.out.println("isopen : " + isopen);
		System.out.println("userid : " + userid);
		
		
		/**
		 * 获取json字符串里的content(段落)和img(插图)
		 */
		if (rootJObj.has("contents")) {
			JSONArray contents = rootJObj.getJSONArray("contents");
			if (contents != null) {
				for(int i=0 ; i<contents.length() ; i++){
					Content content = new Content();
					content.setUserId(userid);
					content.setAuthor(author);
					JSONObject contentJson = (JSONObject) contents.get(i);
					if(contentJson.has("fontColor")){
						content.setFontColor(contentJson.getString("fontColor"));
					}else{
						content.setFontColor("#000000");
					}
					if(contentJson.has("fontSize")){
						content.setFontSize(contentJson.getInt("fontSize"));
					}else{
						content.setFontSize(12);
					}
					if(contentJson.has("serialNum")){
						content.setSerialNum(contentJson.getInt("serialNum"));
					}else{
						return "serialnum tag is not exist!";
					}
					if(contentJson.has("content")){
						content.setContent(contentJson.getString("content"));
					}else{
						return "content tag is not exist!";
					}
					System.out.println("***********************************************");
					System.out.println("color : " + content.getFontColor());
					System.out.println("size : " + content.getFontSize());
					System.out.println("serialnum : " + content.getSerialNum());
					System.out.println("userid : " + content.getUserId());
					System.out.println("content : " + content.getContent());
					System.out.println("author : " + content.getAuthor());
					System.out.println("***********************************************");
					contentList.add(content);
				}
			}
		}else {
			return "contents tag is not exist!";
		}
		
		System.out.println("---------------------------------");
		
		return "ok";
	}
}


